package Reports::ExtReport;

use strict;
use warnings;

my $VERSION = '1.01';
our @EXPORT_OK = qw(gen_ext_report);
use base qw(Exporter);

sub gen_ext_report{
	my %data = %{$_[0]};
	my $ext_report_file = $_[1];
	my $limit_ascore = $_[2];
	my $is_tmt_or_itraq = $_[3];

	my $text_head;
	if ($is_tmt_or_itraq eq "itraq"){
		$text_head = "Consensus peptide w/o mods\tConsensus peptide with mods\t" .
					 "Consensus z\tConsensus DeltaMz\tConsensus proteins\tPhosphorylated\t_\t" .
					 "itraq114\titraq115\titraq116\titraq117\t_\t" .
					 "Ascore revised peptide\tAscore1\tAscore2\tAscore3\t_";
	}
	elsif ($is_tmt_or_itraq eq "tmt"){
		$text_head = "Consensus peptide w/o mods\tConsensus peptide with mods\tConsensus z\t" .
					 "Consensus DeltaMz\tConsensus proteins\tPhosphorylated\t_\t" .
					 "TMT_126\tTMT_127\tTMT_128\tTMT_129\tTMT_130\tTMT_131\t" .
					 "_\tAscore revised peptide\tAscore1\tAscore2\tAscore3\t_";
	}
	else{
		$text_head = "Consensus peptide w/o mods\tConsensus peptide with mods\t" .
					 "Consensus z\tConsensus DeltaMz\tConsensus proteins\tPhosphorylated\t_\t" .
					 "Ascore revised peptide\tAscore1\tAscore2\tAscore3\t_";
	}

	open my $GLOBAL, '>', $ext_report_file;
	print $GLOBAL "File\tFirst Scan\tLast scan\tMS\t#Engines\tB\tO\tP\tRejected pept\t" .
				  "Seq pept w/o mods\tSeq pept mods\tSeq z\tSeq Actual Mass\t" .
				  "Seq DeltaMass\tSeq XCorr\tSeq DeltaCn\tSeq Sp\tSeq RSp\tSeq Ions\tSeq D\t" .
				  "Seq DB 1st match\tSeq Count\t";
	print $GLOBAL "_\t";
	print $GLOBAL "Omssa pept w/o mods\tOmssa pept mods\tz\t" .
				 "Omssa Calc Neutral Pep Mass\tOmssa Mass diff\tOmssa Pvalue\t" .
				 "Omssa Evalue\tOmssa DB 1st match\t";
	print $GLOBAL "_\t";
	print $GLOBAL "Phenyx pept w/o mods\tPhenyx pept mods\tPhenyx z\t" .
				 "Phenyx mz\tPhenyx Deltamz\tPhenyx z-score\tPhenyx Validity\t" .
				 "Phenyx DB 1st match\tPhenyx Count\t";
	print $GLOBAL "_\t";
	print $GLOBAL $text_head;
	print $GLOBAL "\n";

	foreach my $spec(keys %data){
		#Solo reporto los consenso en el report Global!!!!
		unless(defined $data{$spec}{'consensus_mod'}){
			next;
		}
		my $phospho = 0;
		my $pept_conteo_fosfs = $data{$spec}{'consensus_mod'};
		if ($pept_conteo_fosfs =~ /(\(21\)|\(21\,|21\)|\(23\)|\(23\,|23\))/) {
			$phospho = 1;
		}

		if($phospho){
			my ($a1,$a2,$a3) = (0, 0, 0);
			if(defined $data{$spec}{'ascore_data'}){
				if(defined $data{$spec}{'ascore_data'}{'ascore'}[0]){
					if($data{$spec}{'ascore_data'}{'ascore'}[0] > $limit_ascore){
						$a1 = 1;
					}
				}
				else{
					$a1 = 6666;
				}
				if(defined $data{$spec}{'ascore_data'}{'ascore'}[1]){
					if($data{$spec}{'ascore_data'}{'ascore'}[1] > $limit_ascore){
						$a2 = 1;
					}
				}
				if(defined $data{$spec}{'ascore_data'}{'ascore'}[2]){
					if($data{$spec}{'ascore_data'}{'ascore'}[2] > $limit_ascore){
						$a3 = 1;
					}
				}
			}
		}
		my $dummy = $spec;
		$dummy =~ s/\./\t/g;
		print $GLOBAL $dummy, "\t";
		print $GLOBAL $data{$spec}{'ms'}, "\t";

		my $engines = 0;
		my %contador;
		if (defined $data{$spec}{'sequest'}{'unmodified_sequence'}){
			$contador{$data{$spec}{'sequest'}{'unmodified_sequence'}} = 1;
			$engines++;
		}
		if (defined $data{$spec}{'omssa'}{'unmodified_sequence'}){
			$contador{$data{$spec}{'omssa'}{'unmodified_sequence'}} = 1;
			$engines++;
		}
		if (defined $data{$spec}{'phenyx'}{'unmodified_sequence'}){
			$contador{$data{$spec}{'phenyx'}{'unmodified_sequence'}} = 1;
			$engines++;
		}
		$engines = $engines - (keys %contador) + 1;
		print $GLOBAL $engines, "\t";

		my ($seq, $oms, $phe) = (0, 0, 0);
		my $rejected_peptide = '';

		if (defined $data{$spec}{'sequest'}{'unmodified_sequence'}){
			if ($data{$spec}{'sequest'}{'unmodified_sequence'} eq $data{$spec}{'consensus'}){
				$seq = 1;
			}
			else{
				$rejected_peptide = $data{$spec}{'sequest'}{'modified_sequence'};
			}
		}
		if (defined $data{$spec}{'omssa'}{'unmodified_sequence'}){
			if ($data{$spec}{'omssa'}{'unmodified_sequence'} eq $data{$spec}{'consensus'}){
				$oms = 1;
			}
			else{
				$rejected_peptide = $data{$spec}{'omssa'}{'modified_sequence'};
			}
		}
		if (defined $data{$spec}{'phenyx'}{'unmodified_sequence'}){
		   if ($data{$spec}{'phenyx'}{'unmodified_sequence'} eq $data{$spec}{'consensus'}){
				$phe = 1;
			}
			else{
				$rejected_peptide = $data{$spec}{'phenyx'}{'modified_sequence'};
			}
		}
		print $GLOBAL $seq, "\t", $oms, "\t", $phe, "\t", $rejected_peptide, "\t";
		if ((defined $data{$spec}{'sequest'}{'unmodified_sequence'})and
			($data{$spec}{'sequest'}{'unmodified_sequence'} eq $data{$spec}{'consensus'})
		   ){
			print $GLOBAL $data{$spec}{'sequest'}{'unmodified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'modified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'z'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'actual_mass'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'deltamass'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'xcorr'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'deltacn'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'sp'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'rsp'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'ions'}, "\t";
			print $GLOBAL $data{$spec}{'sequest'}{'d'}, "\t";
			if(defined $data{$spec}{'sequest'}{'db_first_match'}){
				print $GLOBAL $data{$spec}{'sequest'}{'db_first_match'},"\t";
			}
			else{
				print $GLOBAL 'NA', "\t";
			}
			print $GLOBAL $data{$spec}{'sequest'}{'count'}, "\t";
		}
		else{
			for(my $i=0; $i<13; $i++){
				print $GLOBAL "\t";
			}
		}
		print $GLOBAL "+\t";

		if ((defined $data{$spec}{'omssa'}{'unmodified_sequence'})and
			($data{$spec}{'omssa'}{'unmodified_sequence'} eq $data{$spec}{'consensus'})
		   ){
			print $GLOBAL $data{$spec}{'omssa'}{'unmodified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'modified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'z'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'calc_neutral_pep_mass'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'massdiff'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'pvalue'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'expect'}, "\t";
			print $GLOBAL $data{$spec}{'omssa'}{'db_first_match'}, "\t";
		}
		else{
			for(my $i=0;$i<8;$i++){
				print $GLOBAL "\t";
			}
		}
		print $GLOBAL "+\t";

		if ((defined $data{$spec}{'phenyx'}{'unmodified_sequence'})and
			($data{$spec}{'phenyx'}{'unmodified_sequence'} eq $data{$spec}{'consensus'})
		   ){
			print $GLOBAL $data{$spec}{'phenyx'}{'unmodified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'modified_sequence'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'z'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'mz'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'deltamz'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'zscore'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'validity'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'db_first_match'}, "\t";
			print $GLOBAL $data{$spec}{'phenyx'}{'count'}, "\t";
		}
		else{
			for(my $i=0;$i<9;$i++){
				print $GLOBAL "\t";
			}
		}
		print $GLOBAL "+\t";

		print $GLOBAL $data{$spec}{'consensus'}, "\t";
		print $GLOBAL $data{$spec}{'consensus_mod'}, "\t";
		print $GLOBAL $data{$spec}{'consensus_z'}, "\t";
		if(defined $data{$spec}{'consensus_deltamz'}){
			print $GLOBAL $data{$spec}{'consensus_deltamz'}, "\t";
		}
		else{
			print $GLOBAL "0\t";
		}

		print $GLOBAL $data{$spec}{'consensus_proteins'},"\t";
		print $GLOBAL $phospho,"\t+\t";

		#datos de cuantificacion
		if ($is_tmt_or_itraq eq "itraq"){
			if (defined $data{$spec}{'itraq_info'}){
				my $itraq_info = $data{$spec}{'itraq_info'};
				my ($i114, $i115, $i116, $i117);
				if(($i114, $i115, $i116, $i117) = split /\|/, $itraq_info){
					print $GLOBAL "$i114\t$i115\t$i116\t$i117\t";
				}
				else {
					print $GLOBAL "0\t" x 4;
				}
			}
			else {
				print $GLOBAL "0\t" x 4;
			}
			print $GLOBAL "+\t";
		}
		elsif ($is_tmt_or_itraq eq "tmt"){
			if(defined $data{$spec}{'tmt_info'}){
				my $tmt_info = $data{$spec}{'tmt_info'};
				my ($t126,$t127,$t128,$t129,$t130,$t131);
				if(($t126,$t127,$t128,$t129,$t130,$t131) = split /\|/, $tmt_info){
					print $GLOBAL "$t126\t$t127\t$t128\t$t129\t$t130\t$t131\t";
				}
				else{
					print $GLOBAL "0\t" x 6;
				}
			}
			else{
				print $GLOBAL "0\t" x 6;
			}
			print $GLOBAL "+\t";
		}

		my $cuenta_ascores = 0;
		if(defined $data{$spec}{'ascore_data'}{'ascore'}){
			print $GLOBAL $data{$spec}{'ascore_data'}{'peptide'}, "\t";
			my @ascores = @{$data{$spec}{'ascore_data'}{'ascore'}};
			foreach(@ascores){
				$cuenta_ascores++;
				print $GLOBAL sprintf("%.2f", $_), "\t";
			}
			for (my $t=0; $t<(3-$cuenta_ascores); $t++){
				print $GLOBAL "\.\t";
			}
		}
		else{
			if(defined $data{$spec}{'consensus_mod'}){
				if($data{$spec}{'consensus_mod'} =~ /(\(21\)|\(23\))/){
					print $GLOBAL $data{$spec}{'consensus_mod'}, "\.\t" x 4;
				}
				else{
					print $GLOBAL "\.\t" x 4;
				}
			}
			else{
				print $GLOBAL "\.\t" x 4;
			}
		}
		print $GLOBAL "+\n";
	}
	close $GLOBAL;
	return 1;
}

1;            #282

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 LIMITATIONS

=head1 SEE ALSO

=cut
